package com.boot.validate.exception;

import java.util.List;

import com.boot.model.Result;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 切面，统一异常处理
 */
@ControllerAdvice
@ResponseBody
public class ValidateExceptionHandler {

    @ExceptionHandler(value = BindException.class)
    public Result bindExceptionHandler(BindException bindException) {
        List<ObjectError> errors = bindException.getAllErrors();
        return new Result().setCode("-1").setErrorMsg(errors.get(0).getDefaultMessage());
    }
}
